// 地图管理系统
class MapManager {
    constructor() {
        this.maps = new Map();
        this.currentMapId = 'forest_1';
        this.mapTransitions = new Map();
        this.initializeMaps();
    }

    // 初始化所有地图
    initializeMaps() {
        // 森林地图 - 新手区域
        this.maps.set('forest_1', {
            id: 'forest_1',
            name: '新手森林',
            level: 1,
            description: '适合新手冒险者的安全森林',
            size: { width: 40, height: 40 },
            monsterTypes: ['SLIME', 'SKELETON'],
            monsterLevelRange: [1, 3],
            monsterCount: 8,
            spawnRate: 0.008,
            itemDropRate: 0.6,
            background: '#2d5016',
            tileTypes: {
                grass: { color: '#4CAF50', walkable: true },
                tree: { color: '#2E7D32', walkable: false },
                water: { color: '#2196F3', walkable: false },
                rock: { color: '#795548', walkable: false }
            },
            portals: [
                { x: 5, y: 5, targetMap: 'cave_1', name: '神秘洞穴入口' },
                { x: 35, y: 35, targetMap: 'forest_2', name: '深入森林' }
            ]
        });

        // 森林地图2 - 中级区域
        this.maps.set('forest_2', {
            id: 'forest_2',
            name: '深林深处',
            level: 2,
            description: '更危险的森林深处，怪物更强',
            size: { width: 50, height: 50 },
            monsterTypes: ['SKELETON', 'ORC'],
            monsterLevelRange: [3, 6],
            monsterCount: 12,
            spawnRate: 0.01,
            itemDropRate: 0.7,
            background: '#1a3d0a',
            tileTypes: {
                grass: { color: '#388E3C', walkable: true },
                tree: { color: '#1B5E20', walkable: false },
                water: { color: '#1976D2', walkable: false },
                rock: { color: '#5D4037', walkable: false },
                swamp: { color: '#4A148C', walkable: true }
            },
            portals: [
                { x: 5, y: 5, targetMap: 'forest_1', name: '返回新手森林' },
                { x: 45, y: 45, targetMap: 'mountain_1', name: '前往山脉' }
            ]
        });

        // 洞穴地图 - 中级区域
        this.maps.set('cave_1', {
            id: 'cave_1',
            name: '神秘洞穴',
            level: 2,
            description: '黑暗的洞穴，充满未知的危险',
            size: { width: 35, height: 35 },
            monsterTypes: ['SKELETON', 'ORC'],
            monsterLevelRange: [4, 7],
            monsterCount: 10,
            spawnRate: 0.012,
            itemDropRate: 0.8,
            background: '#1a1a1a',
            tileTypes: {
                stone: { color: '#424242', walkable: true },
                wall: { color: '#212121', walkable: false },
                crystal: { color: '#9C27B0', walkable: false },
                lava: { color: '#FF5722', walkable: false }
            },
            portals: [
                { x: 5, y: 5, targetMap: 'forest_1', name: '返回森林' },
                { x: 30, y: 30, targetMap: 'cave_2', name: '深入洞穴' }
            ]
        });

        // 洞穴地图2 - 高级区域
        this.maps.set('cave_2', {
            id: 'cave_2',
            name: '洞穴深处',
            level: 3,
            description: '洞穴的最深处，极其危险',
            size: { width: 30, height: 30 },
            monsterTypes: ['ORC', 'DRAGON'],
            monsterLevelRange: [6, 10],
            monsterCount: 8,
            spawnRate: 0.015,
            itemDropRate: 0.9,
            background: '#0d0d0d',
            tileTypes: {
                stone: { color: '#616161', walkable: true },
                wall: { color: '#424242', walkable: false },
                crystal: { color: '#E91E63', walkable: false },
                lava: { color: '#FF9800', walkable: false },
                treasure: { color: '#FFD700', walkable: true }
            },
            portals: [
                { x: 5, y: 5, targetMap: 'cave_1', name: '返回上层洞穴' }
            ]
        });

        // 山脉地图 - 高级区域
        this.maps.set('mountain_1', {
            id: 'mountain_1',
            name: '险峻山脉',
            level: 3,
            description: '高耸的山脉，强大的怪物栖息地',
            size: { width: 45, height: 45 },
            monsterTypes: ['ORC', 'DRAGON'],
            monsterLevelRange: [5, 9],
            monsterCount: 10,
            spawnRate: 0.012,
            itemDropRate: 0.85,
            background: '#3e2723',
            tileTypes: {
                rock: { color: '#8D6E63', walkable: true },
                mountain: { color: '#5D4037', walkable: false },
                snow: { color: '#ECEFF1', walkable: true },
                ice: { color: '#B3E5FC', walkable: false },
                peak: { color: '#FFEB3B', walkable: true }
            },
            portals: [
                { x: 5, y: 5, targetMap: 'forest_2', name: '返回森林' },
                { x: 40, y: 40, targetMap: 'volcano_1', name: '前往火山' }
            ]
        });

        // 火山地图 - 顶级区域
        this.maps.set('volcano_1', {
            id: 'volcano_1',
            name: '火山口',
            level: 4,
            description: '炽热的火山口，最强大的怪物聚集地',
            size: { width: 40, height: 40 },
            monsterTypes: ['DRAGON', 'PHOENIX'],
            monsterLevelRange: [8, 15],
            monsterCount: 6,
            spawnRate: 0.02,
            itemDropRate: 1.0,
            background: '#bf360c',
            tileTypes: {
                lava: { color: '#FF5722', walkable: false },
                rock: { color: '#D84315', walkable: true },
                obsidian: { color: '#212121', walkable: false },
                fire: { color: '#FF9800', walkable: false },
                treasure: { color: '#FFD700', walkable: true }
            },
            portals: [
                { x: 5, y: 5, targetMap: 'mountain_1', name: '返回山脉' }
            ]
        });

        console.log('地图系统初始化完成，共创建', this.maps.size, '个地图');
    }

    // 获取当前地图信息
    getCurrentMap() {
        return this.maps.get(this.currentMapId);
    }

    // 切换到指定地图
    switchMap(mapId, playerX = null, playerY = null) {
        const targetMap = this.maps.get(mapId);
        if (!targetMap) {
            console.error('地图不存在:', mapId);
            return false;
        }

        const currentMap = this.getCurrentMap();
        console.log(`从 ${currentMap.name} 切换到 ${targetMap.name}`);

        this.currentMapId = mapId;

        // 如果指定了坐标，使用指定坐标，否则使用默认坐标
        if (playerX !== null && playerY !== null) {
            return { x: playerX, y: playerY };
        }

        // 返回默认出生点（地图中心）
        return {
            x: targetMap.size.width * 16, // 16是TILE_SIZE的一半
            y: targetMap.size.height * 16
        };
    }

    // 检查地图传送门
    checkPortal(playerX, playerY) {
        const currentMap = this.getCurrentMap();
        if (!currentMap.portals) return null;

        for (const portal of currentMap.portals) {
            const distance = Math.sqrt(
                Math.pow(playerX / 32 - portal.x, 2) + 
                Math.pow(playerY / 32 - portal.y, 2)
            );
            
            if (distance < 2) { // 2格范围内触发传送门
                return portal;
            }
        }
        return null;
    }

    // 获取地图推荐等级
    getRecommendedLevel(mapId) {
        const map = this.maps.get(mapId);
        return map ? map.level : 1;
    }

    // 获取所有地图列表
    getAllMaps() {
        return Array.from(this.maps.values());
    }

    // 获取可访问的地图（基于玩家等级）
    getAccessibleMaps(playerLevel) {
        return this.getAllMaps().filter(map => map.level <= playerLevel + 1);
    }
}

// 扩展怪物类型，添加高级怪物
const EXTENDED_MONSTER_TYPES = {
    SLIME: {
        name: '史莱姆',
        health: 50,
        damage: 5,
        exp: 20,
        color: '#4CAF50',
        dropRate: 0.8,
        level: 1
    },
    SKELETON: {
        name: '骷髅战士',
        health: 80,
        damage: 8,
        exp: 35,
        color: '#9E9E9E',
        dropRate: 0.85,
        level: 2
    },
    ORC: {
        name: '兽人战士',
        health: 120,
        damage: 12,
        exp: 50,
        color: '#795548',
        dropRate: 0.9,
        level: 3
    },
    DRAGON: {
        name: '幼龙',
        health: 200,
        damage: 25,
        exp: 100,
        color: '#FF5722',
        dropRate: 0.95,
        level: 5
    },
    PHOENIX: {
        name: '火凤凰',
        health: 300,
        damage: 35,
        exp: 150,
        color: '#FF9800',
        dropRate: 1.0,
        level: 7
    }
};

// 导出地图管理器
window.MapManager = MapManager;
window.EXTENDED_MONSTER_TYPES = EXTENDED_MONSTER_TYPES;

